﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;
using MasterTemplate = Cms.Web.Framework.Controls.MasterTemplate;

namespace Cms.Web.UI.Admin
{
    public partial class RssFeeds :  AdminPage, IRssFeedsView
    {
        private RssFeedsPresenter _presenter;

        #region IRssFeedsView Members

        public void SetRssFeeds(IList<RssFeed> rssFeeds)
        {
            rpRssFeeds.DataSource = rssFeeds;
            rpRssFeeds.DataBind();
            pnlNoRssFeeds.Visible = rssFeeds.Count == 0;
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/RssFeeds.xml"; }
        }
        
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new RssFeedsPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('rssFeedPaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }


        protected void rpRssFeeds_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var rssFeed = (RssFeed) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litName = (Literal) e.Item.FindControl("litName");
            var litFilename = (Literal) e.Item.FindControl("litFilename");
            var litCategories = (Literal)e.Item.FindControl("litCategories");
            var litContentDisplayMode = (Literal)e.Item.FindControl("litContentDisplayMode");
            var litRequiredUserGroups = (Literal)e.Item.FindControl("litRequiredUserGroups");
            var litUrl = (Literal)e.Item.FindControl("litUrl");
            var hlUrl = (HyperLink)e.Item.FindControl("hlUrl");
            var litSystemId = (Literal)e.Item.FindControl("litSystemId");
            litID.Text = "<span id=\"M" + rssFeed.ID + "\">";
            litName.Text = rssFeed.Name;
            litSystemId.Text = rssFeed.ID.ToString();
            litFilename.Text = rssFeed.Filename;
            litCategories.Text = string.Join(", ", from c in rssFeed.Categories select c.Name);
            litRequiredUserGroups.Text = string.Join(", ", from g in rssFeed.RequiredUserGroups select g.Name);
            litUrl.Text = "http://" + rssFeed.Site.Domain + "/rss/" + rssFeed.Filename;
            hlUrl.NavigateUrl = litUrl.Text;
            litContentDisplayMode.Text = rssFeed.ContentDisplayMode == RssFeedContentDisplayMode.Complete 
                                            ? GetText(this, "llContentDisplayModeComplete") 
                                            : GetText(this, "llContentDisplayModeTruncated");
        }


        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.DeleteRssFeed(int.Parse(hidDeleteID.Value.Substring(1)));
            upnlRssFeeds.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initList",
                                                            "initList();", true);
        }
    }
}